/**
 * Project Name:UIMS
 * File Name:PortalAction.java
 * Package Name:com.bgi.uims.module.sys.Portal.action
 * CreateDate:2015年3月24日 上午10:19:52
 * Copyright (c) 2015, BGI_DX All Rights Reserved.
 */     

package com.bgi.uims.module.sys.portal.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.bgi.uims.common.action.BaseAction;
import com.bgi.uims.common.dto.PageVo;
import com.bgi.uims.module.sys.portal.dto.Portal;
import com.bgi.uims.module.sys.portal.dto.UserDesktop;
import com.bgi.uims.module.sys.portal.service.PortalSysService;
import com.bgi.uims.module.sys.user.dto.User;
import com.bgi.uims.pub.utils.Constant;
import com.bgi.uims.pub.utils.PublicMethod;

/**
 * ClassName:PortalAction 
 * Function: TODO 
 * Reason:	 TODO 
 * @CreateDate:2015年3月24日 上午10:19:52
 * @author   tantianhua
 * @version  
 * @since    JDK 1.7
 * @see 	 
 */
@Controller
@Scope("prototype")
public class PortalAction extends BaseAction {
	
	/**  
	 * serialVersionUID:TODO（用一句话描述这个变量表示什么）  
	 */   
	private static final long serialVersionUID = -700919983611115341L;

	private PageVo pageVo = new PageVo();
	
	private Portal portal;
	
	private UserDesktop userDesktop; 
	
	@Resource
	private PortalSysService portalSysService;

	public PageVo getPageVo() {
		return pageVo;
	}

	public void setPageVo(PageVo pageVo) {
		this.pageVo = pageVo;
	}
	public Portal getPortal() {
		return portal;
	}

	public void setPortal(Portal portal) {
		this.portal = portal;
	}
	
	public UserDesktop getUserDesktop() {
		return userDesktop;
	}

	public void setUserDesktop(UserDesktop userDesktop) {
		this.userDesktop = userDesktop;
	}
	
	/**
	 * 
	 * @Description：  查询面板信息
	 * @author: tantianhua
	 * @CreateDate:2015年3月24日 上午10:19:52
	 * @version: V1.0
	 */
	public String findPortal()
	{
		if(portal==null)
		{
			portal=new Portal();
		}
		portal.getPage(this);
		pageVo=portalSysService.queryPortal(portal);
		session.put(Constant.OPERATIONMSG, PublicMethod.operationMsg(Constant.SELECT,Constant.PORTALMODULES,portal, null));
		pageVo.setSuccess(true);
		return SUCCESS;
	}
	
	/**
	 * 
	 * @Description：  查询单个面板信息
	 * @author: tantianhua
	 * @CreateDate:2015年3月24日 上午10:19:52
	 * @version: V1.0
	 */
	public String findPortalById()
	{
		pageVo=new PageVo();
		pageVo.setObject(portalSysService.findPortalById(portal));
		session.put(Constant.OPERATIONMSG, PublicMethod.operationMsg(Constant.SELECT,Constant.PORTALMODULES,portal, null));
		pageVo.setSuccess(true);
		return SUCCESS;
	}
	
	/**
	 * 
	 * @Description：  保存面板信息
	 * @author: tantianhua
	 * @CreateDate:2015年3月24日 上午10:19:52
	 * @version: V1.0
	 */
	public String savePortal()
	{
		pageVo=new PageVo();
		if(portal==null){
			pageVo.setSuccess(false);
			pageVo.setMsg("面板名称不允许为空!");
		}
		else
		{
			//获取登录的session
			User user=(User) session.get(Constant.LOGINUSER);
			if(portal.getPortalId()!=null&&!"".equals(portal.getPortalId())){
				portal.setPortalState("1"); 
				Portal oldPortal = portalSysService.findPortalById(portal);
				portal.setLastModifiedUser(user.getRealName());
				pageVo=portalSysService.savePortal(portal);
				session.put(Constant.OPERATIONMSG, PublicMethod.operationMsg(Constant.UPDATE,Constant.PORTALMODULES , oldPortal, portal));
			}else{
				portal.setPortalState("1");
				portal.setCreateUser(user.getRealName());
				pageVo=portalSysService.savePortal(portal);
				session.put(Constant.OPERATIONMSG, PublicMethod.operationMsg(Constant.INSERT,Constant.PORTALMODULES , portal, null));

			}
		}
		return SUCCESS;
	}
	
	/**
	 * 
	 * @Description：  删除面板信息
	 * @author: tantianhua
	 * @CreateDate:2015年3月24日 上午10:19:52
	 * @version: V1.0
	 */
	public String deletePortal(){
		pageVo=new PageVo();
		Portal oldPortal = portalSysService.findPortalById(portal);
		portalSysService.deletePortalRelation(portal);
		boolean f=  portalSysService.deletePortal(portal);
		session.put(Constant.OPERATIONMSG, PublicMethod.operationMsg(Constant.DELETE,Constant.PORTALMODULES , oldPortal, portal));
		pageVo.setSuccess(f);
		return SUCCESS;
	}
	
	/**
	 * 
	 * @Description：  更新面板状态
	 * @author: tantianhua
	 * @CreateDate:2015年3月24日 上午10:19:52
	 * @version: V1.0
	 */
	public String updatePortalStatus(){
		pageVo=new PageVo();
		Portal oldPortal = new Portal();
		Portal newPortal = new Portal();
		
		if("0".equals(portal.getPortalState())){
			
			oldPortal.setPortalId(portal.getPortalId());
			oldPortal.setPortalState("0");
			newPortal.setPortalId(portal.getPortalId());
			newPortal.setPortalState("1");
			portal.setPortalState("1");
		}
		else if("1".equals(portal.getPortalState())){
			
			oldPortal.setPortalId(portal.getPortalId());
			oldPortal.setPortalState("1");
			newPortal.setPortalId(portal.getPortalId());
			newPortal.setPortalState("0");
			portal.setPortalState("0");
		}
		
		User users=(User) session.get(Constant.LOGINUSER);
		portal.setLastModifiedUser(users.getRealName());
		pageVo.setObject(portalSysService.updatePortalStatus(portal));
		session.put(Constant.OPERATIONMSG, PublicMethod.operationMsg(Constant.UPDATE,Constant.PORTALMODULES , oldPortal, newPortal));
		pageVo.setSuccess(true);
		return SUCCESS;
	}
	
	/**
	 * 
	 * @Description：  初始化首页面板
	 * @author: wuyaohong
	 * @CreateDate:2015年3月30日 下午3:40:36
	 * @version: V1.0
	 */
	public String queryUserDesktopOrder(){
		User user = (User) session.get(Constant.LOGINUSER);
		//用户保存的面板配置
		userDesktop = portalSysService.queryDesktopByUser(user);
		String desktopConfig = null;
		if(userDesktop != null && !StringUtils.isEmpty(userDesktop.getOrderData())){ 
			desktopConfig = userDesktop.getOrderData();
			userDesktop.setRolesIds(user.getRolesId().split(","));
			//角色不允许的面板
			pageVo=portalSysService.queryPortalNotInRole(userDesktop);
			//移除用户配置中，角色不允许的面板
			for(Object obj : pageVo.getRows()){
				Portal p = (Portal)obj;
				String portalCode = p.getPortalCode();
				if(portalCode.equals("pd_noticePanel")){
					continue;
				}else{
					desktopConfig = desktopConfig.replaceAll(portalCode, "");
				}
			}
		} else {
			userDesktop = new UserDesktop();
			pageVo = new PageVo();
			desktopConfig = "[]";
		}
		//返回用户桌面配置
		userDesktop.setOrderData(desktopConfig);
		userDesktop.setRolesId(user.getRolesId());//用userid传递rolesId
		pageVo.setObject(userDesktop);
		
		return SUCCESS;
	}
	
	/**
	 * 
	 * @Description：  角色允许的面板
	 * @author: wuyaohong
	 * @CreateDate:2015年3月30日 下午3:41:06
	 * @version: V1.0
	 */
	@SuppressWarnings("unused")
	public String queryUserDesktopByRole(){
		User user = (User) session.get(Constant.LOGINUSER);
		userDesktop.setRolesIds(userDesktop.getRolesId().split(","));
		pageVo = portalSysService.queryPortalByRole(userDesktop);
		List<Portal> portals = new ArrayList<Portal>();
		//移除系统固定的面板
		for(Object obj : pageVo.getRows()){
			Portal p = (Portal)obj;
			if(p.getPortalCode().equals("pd_noticePanel")){
				continue;
			}else{
				portals.add(p);
			}
		}
		pageVo.setRows(portals);
		return SUCCESS;
	}
	
	/**
	 * 
	 * @Description：  保存和更新首页面板位置
	 * @author: wuyaohong
	 * @CreateDate:2015年3月30日 下午3:41:13
	 * @version: V1.0
	 */
	public String saveUserDesktopOrder(){
		User user = (User) session.get(Constant.LOGINUSER);
		userDesktop.setUserId(user.getUserId());
		if(StringUtils.isEmpty(userDesktop.getUserPanelId())){
			portalSysService.addUserDesktop(userDesktop);
		} else {
			try {
				userDesktop.setUserId(user.getUserId());
				List<String> odlOrder = getPortalNameList(portalSysService.queryDesktopByUser(user).getOrderData());
				List<String> newOrder = getPortalNameList(userDesktop.getOrderData());
				StringBuffer message = new StringBuffer();
				message.append("更新内容为：");
				if(newOrder.size() > 0){
					if(odlOrder.size() > 0){
						for(int i = 0; i < newOrder.size(); i++){
							for(String s : odlOrder){
								if(s.equals(newOrder.get(i))){
									continue;
								}else{
									if(!newOrder.contains(s)){
										if(message.indexOf(s) < 0){
											message.append("删除了面板'"+s+"'，");
										}
									}
									if(!odlOrder.contains(newOrder.get(i))){
										if(message.indexOf(s) < 0){
											message.append("新关联了面板'"+s+"'，");
										}
									}
									continue;
								}
							}
						}
					}else{
						for(int i = 0; i < newOrder.size(); i++){
							message.append("新关联了面板'"+newOrder.get(i)+"'，");
						}
					}
				}else{
					message.append("删除了面板");
					for(String s : odlOrder){
						message.append("删除了面板'"+s+"'，");
					}
				}
				String[] msgStr=new String[5];
				msgStr[0] = "sys_user_portal";
				msgStr[1] = user.getUserId();
				msgStr[2] = "3";
				msgStr[3] = message.toString();
				//不需要存太长
				if(msgStr[3].length()>512)
				{
					msgStr[3]=msgStr[3].substring(0, 508)+"....";
				}
				msgStr[4] = Constant.PORTALMODULES;
				session.put(Constant.OPERATIONMSG, msgStr);
				pageVo.setSuccess(portalSysService.updateUserDesktop(userDesktop));
			} catch (Exception e) {
				e.printStackTrace();
				pageVo.setSuccess(false);
				pageVo.setMsg("保存失败！");
			}
		}
		return SUCCESS;
	}
	
	/**
	 * 
	 * @Description：  面板排序转换为String list
	 * @author: wuyaohong
	 * @CreateDate:2015年4月9日 上午10:23:58
	 * @version: V1.0
	 */
	public List<String> getPortalNameList(String orderDate){
		List<String> list = new ArrayList<String>();
		if(orderDate.length() > 0){
			orderDate = orderDate.replace("[", "").replace("]", "").replace("]", "").replace("\"", "");
			String[] data = orderDate.split(",");
			for(int i = 0; i < data.length; i++){
				list.add(data[i]);
			}
		}
		return list;
	}
	

}
